<?php

declare(strict_types=1);

namespace App\Controller\MchApi;

use App\Exception\XZXValidationException;
use App\Service\ProtocolInfoService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Middleware\MchApi\MchJWTAuthMiddleware;

/**
 * Class ProtocolInfoController
 * @package App\Controller
 * @Controller(prefix="/mch/api/protocolInfo")
 */
class ProtocolInfoController extends MchBaseController
{
    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var ProtocolInfoService
     */
    protected $ProtocolInfoService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale()
    {
        if($this->request->header('locale') == 'en')
        {
            $this->translator->setLocale('en');
        }
    }   

    /**
     * 查看
     * @PostMapping(path = "get")
     * @@Middleware(MchJWTAuthMiddleware::class)
     */
    public function get(RequestInterface $request)
    {
        $this->setlocale();

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                // 'id' => 'required|uuid',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        return $this->ProtocolInfoService->getForMch($param);
    }

    // /**
    //  * 编辑
    //  * @PostMapping(path = "edit")
    //  * @Middleware(AdminJWTAuthMiddleware::class)
    //  */
    // public function edit(RequestInterface $request)
    // {
    //     $this->setlocale();

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'id'            => 'required|uuid',
    //             'protocolName'  => 'required',
    //             'url'           => 'required|url',
    //         ]
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     $ext = $this->request->getAttribute("ext");
    //     $param['createBy'] =  $ext['createBy'];
    //     return $this->ProtocolInfoService->edit($param);
    // }

    // /**
    //  * 添加
    //  * @PostMapping(path = "add")
    //  * @Middleware(AdminJWTAuthMiddleware::class)
    //  */
    // public function add(RequestInterface $request)
    // {
    //     $this->setlocale();

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'protocolName'  => 'required',
    //             'url'           => 'required|url',
    //         ]
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     $ext = $this->request->getAttribute("ext");
    //     $param['createBy'] =  $ext['createBy'];
    //     return $this->ProtocolInfoService->add($param);
    // }

    // /**
    //  * 查询
    //  * @PostMapping(path = "list")
    //  * @Middleware(AdminJWTAuthMiddleware::class)
    //  */
    // public function list(RequestInterface $request)
    // {
    //     $this->setlocale();

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'page' => 'required|integer|min:1',
    //             'limit' => 'required|integer|min:1',
    //         ]
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }
        
    //     $param = $request->all();
    //     if(isset($param['search'])){
    //         $search = $param['search'];
    //         CustomUtils::xzxAddSearch($param, $search, 'createTimeRange');
    //         if(isset($param["createTimeRange"]))
    //         {
    //             try {
    //                 $param['createTimeRange'][0] = DateUtils::dateToTimeStamp($param['createTimeRange'][0]);
    //                 $param['createTimeRange'][1] = DateUtils::dateToTimeStamp($param['createTimeRange'][1]);
    //             } catch (\Throwable $th) {
    //                 unset($param['createTimeRange']);
    //             }
    //         }
    //     }

    //     return $this->ProtocolInfoService->list($param);
    // }
   
}